package com.example.learnspring.controller;

import com.example.learnspring.entity.Emp;
import com.example.learnspring.entity.EmpParam;
import com.example.learnspring.entity.Pager;
import com.example.learnspring.entity.Result;
import com.example.learnspring.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
public class EmpController {

    @Autowired
    private EmpService empService;

    @GetMapping("/emps")
    public Result getEmp(EmpParam empParam) {
        log.info("params is {},{}", empParam.getStartTime(), empParam.getEndTime());
        Pager pager = empService.page(empParam);
        return Result.success(pager);
    }

    @PostMapping("/emps")
    public Result addEmp(@RequestBody Emp emp) {
        log.info("params is {},{}", emp.getName(), emp.getCName());
        empService.save(emp);
        return Result.success();
    }

    @GetMapping("/emp")
    public Result getEmpById(Integer id) {
        log.info("params is {},{}", id);
        Emp emp = empService.getEmpById(id);
        return Result.success(emp);
    }
}
